<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-button type="primary">添加角色</el-button>
      <el-table
      border
      stripe
      :data="roleList"
      style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <!-- 一级循环 -->
          <el-row class="bdbot" :class="i==0?'bdtop':''" v-for="(item,i) in scope.row.children" :key="item.id">
            <el-col :span="5">
              <el-tag>{{ item.authName }}</el-tag>
              <i class="el-icon-caret-right"></i>
            </el-col>
            <!-- 二级循环 -->
            <el-col :span="19">
              <el-row :class="i==0?'':'bdtop'" v-for="(item2,i) in item.children" :key="item2.id">
                <el-col :span="6">
                  <el-tag type="success">{{ item2.authName }}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <!-- 三级循环 -->
                <el-col :span="18">
                  <el-tag @close="removeTag(scope.row,item3.id)" closable v-for="item3 in item2.children" :key="item3.id" type="warning">
                      {{ item3.authName }}
                  </el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column
        type="index"
        label="#">
      </el-table-column>
      <el-table-column
        prop="roleName"
        label="角色名称">
      </el-table-column>
      <el-table-column
        prop="roleDesc"
        label="角色描述">
      </el-table-column>
      <el-table-column
        label="操作"
        width="300px">
        <template slot-scope="scope">
        <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        <el-button @click="showSetRights(scope.row)" type="warning" icon="el-icon-setting" size="mini">分配权限</el-button>
        </template>
      </el-table-column>
    </el-table>
    </el-card>
    <!-- 分配角色对话框 -->
    <el-dialog
      @close="resetDefskeys"
      title="分配权限"
      :visible.sync="setRightsDialogVisible"
      width="50%">
      <el-tree
       show-checkbox
       node-key="id"
       ref="tree"
       default-expand-all
       :default-checked-keys="defKeys"
       :data="rightsList"
       :props="treeProps"></el-tree>
      <span slot="footer" class="dialog-footer">
          <el-button @click="setRightsDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="subSetRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      roleList: [],
      setRightsDialogVisible: false,
      rightsList: [],
      treeProps: {
        children: 'children',
        label: 'authName'
      },
      defKeys: [],
      roledId: ''
    }
  },

  created () {
    this.getRoles()
  },

  methods: {
    async getRoles () {
      const { data: res } = await this.$axios.get('roles')
      if (res.meta.status !== 200) return this.$message.error('获取角色列表失败')
      this.roleList = res.data
    },
    removeTag (row, rightid) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        var { data: res } = await this.$axios.delete(`roles/${row.id}/rights/${rightid}`)
        if (res.meta.status !== 200) return this.$message.error('删除角色权限失败')
        this.getRoles()
        this.$message.success('取消权限成功')
        // 将后台返回的角色权限从新赋值给当前角色
        row.children = res.data
      }).catch(() => {
        this.$message('已取消删除')
      })
    },
    async showSetRights (row) {
      this.setRightsDialogVisible = true
      const { data: res } = await this.$axios.get('rights/tree')
      if (res.meta.status !== 200) return this.$message.error('获取角色列表失败')
      this.rightsList = res.data
      this.getAllId(row)
      this.roledId = row.id
    },
    getAllId (row) {
      if (!row.children) return this.defKeys.push(row.id)
      row.children.forEach(row => {
        this.getAllId(row)
      })
    },
    resetDefskeys () {
      this.defKeys = []
    },
    async subSetRights () {
      console.log(this.$refs.tree)
      // 获取所有的全选节点
      const keys = this.$refs.tree.getCheckedKeys()
      // 获取所有的半选节点
      const halfkeys = this.$refs.tree.getHalfCheckedKeys()
      const allkeys = [...keys, ...halfkeys]
      var idStr = allkeys.join(',')
      var { data: res } = await this.$axios.post(`roles/${this.roledId}/rights`, {
        rids: idStr
      })
      if (res.meta.status !== 200) return this.$message.error('角色授权失败')
      console.log(res)
      this.$message.success('角色授权成功')
      this.setRightsDialogVisible = false
      this.getRoles()
    }
  }
}
</script>

<style scoped lang='less'>
.el-tag{
  margin: 7px;
}
.el-row {
  display: flex;
  align-items: center;
}
.bdbot{
  border-bottom:1px solid #eee;
}
.bdtop{
  border-top:1px solid #eee;
}
</style>
